/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ufg.inf.es.agenda.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ufg.inf.es.agenda.Model.Endereco;
import ufg.inf.es.agenda.Model.Pessoa;
import ufg.inf.es.agenda.Model.Telefone;
import ufg.inf.es.agenda.presistencia.impl.GenericDAO;

/**
 *
 * @author wilker
 */
@WebServlet(name = "Consulta", urlPatterns = {"/Consulta"})
public class Consulta extends HttpServlet {

    private String corpo = "<!DOCTYPE html>"
            + "<html lang='pt-BR'>"
            + "    <head>"
            + "        <meta charset='utf-8'>"
            + "        <title>Smart-Agenda</title>"
            + "        <link rel='shortcut icon' href='img/a.png'>"
            + "        <link href='css/bootstrap.css' rel='stylesheet'>"
            + "        <style type='text/css'>"
            + "            body {"
            + "                padding-top: 60px;"
            + "                padding-bottom: 40px;"
            + "            }"
            + "            .sidebar-nav {"
            + "                padding: 9px 0;"
            + "            }"
            + "        </style>"
            + "        <link href='css/bootstrap-responsive.css' rel='stylesheet'>"
            + "    </head>"
            + "    <body>"
            + "        <div class='navbar navbar-fixed-top'>"
            + "            <div class='navbar-inner'>"
            + "                <div class='container-fluid'>"
            + "                    <a class='btn btn-navbar' data-toggle='collapse' data-target='.nav-collapse'>"
            + "                        <span class='icon-bar'></span>"
            + "                        <span class='icon-bar'></span>"
            + "                        <span class='icon-bar'></span>"
            + "                    </a>"
            + "                    <a class='brand' href='Consulta?nome='>Smart-Agenda</a>"
            + "                    <div class='nav-collapse'>"
            + "                        <p class='navbar-text pull-right'>Bem Vindo a nossa <a href='Consulta?nome='>smart-agenda</a></p>"
            + "                    </div>"
            + "                </div>"
            + "            </div>"
            + "            </div>"
            + "        <div class='container-fluid'>"
            + "        <div class='row-fluid'>"
            + "                <div class='span3'>"
            + "                    <div class='well sidebar-nav'>"
            + "                        <ul class='nav nav-list'>"
            + "                            <li class='nav-header'>Cadastro</li>"
            + "                            <li ><a href='novo.html' >Cadastrar Contato</a></li>"
            + "                            <li class='nav-header'>Consulta</li>"
            + "                            <li class='active'><a href='Consulta?nome='>Contatos Geral</a></li>"
            + "                        </ul>"
            + "                    </div><!--/.well -->"
            + "                </div><!--/span-->"
            + "                <div class='span9'>"
            + "                    <div class='hero-unit' >"
            + "                        <form action='Consulta' method='GET' class='well form-search'>"
            + "                            <input type='text' placeholder='Nome da pessoa'  name='nome' class='input-large search-query'>"
            + "                            <button type='submit' class='btn'>Busca</button>"
            + "                        </form>"
            + "                        <table class='table table-bordered'>"
            + "                            <thead>"
            + "                                <tr>"
            + "                                    <th>#</th>"
            + "                                    <th>Nome</th>"
            + "                                    <th>Data nascimento</th>"
            + "                                    <th>CPF</th>"
            + "                                    <th>Telefone</th>"
            + "                                    <th>Endereco</th>"
            + "                                    <th>Cidade</th>"
            + "                                    <th>Estado</th>"
            + "                                    <th>Gerenciador</th>"
            + "                                </tr>"
            + "                            </thead>"
            + "                            <tbody>"
            + "                            <tr>";
    private String rodape = " </tr>"
            + "                            </tbody>"
            + "                        </table>"
            + "                        <ul class='pager'>"
            + "                            <li>"
            + "                                <a href='#'>Anterior</a>"
            + "                            </li>"
            + "                            <li>"
            + "                                <a href='#'>Próximo</a>"
            + "                                        </li>"
            + "                        </ul>"
            + "                    </div>"
            + "                </div>"
            + "            </div>"
            + "            <hr>"
            + "            <footer>"
            + "                <p align='center'>&copy; Wilker de Jesus Machado --- Matricula: 090230</p>"
            + "            </footer>"
            + "        </div>"
            + "    </body>"
            + "</html>";

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String nome = request.getParameter("nome");
        List<Pessoa> pessoas;
        //Realiza consulta no banco sobre informações referentes a pessoa com o nome requisitado
        if (nome != null || !"".equals(nome)) {
            pessoas = GenericDAO.busca(Pessoa.class, "nome LIKE '%" + nome + "%'");
        } else {
            pessoas = GenericDAO.listarTodos(Pessoa.class);
        }
        // List<Pessoa> pessoas = GenericDAO.listarTodos(Pessoa.class);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print(corpo);
        //Exibe informações referentes ao resultado da consulta
        for (Pessoa p : pessoas) {
            out.print("<td>" + p.getId() + "</td>"
                    + "<td>" + p.getNome() + "</td>"
                    + "<td>" + p.getDataNascimento() + "</td>"
                    + "<td>" + p.getCpf() + "</td>");
            String tels = "";
            for (Telefone t : p.getTelefones()) {
                tels = tels + " / " + t.getNumero();
            }
            out.print("<td>" + tels + "</td>");
            String estado = "";
            String cidade = "";
            String end = "";
            for (Endereco e : p.getEnderecos()) {
                estado = e.getEstado();
                cidade = e.getCidade();
                end = e.getRua() + " " + e.getComplemento() + " - " + e.getSetor();
            }
            out.print("<td>" + end + "</td>"
                    + "<td>" + cidade + "</td>"
                    + "<td>" + estado + "</td>"
                    + "<td><a title='Excluir' class='btn btn-danger' href='Excluir?id=" + p.getId() + "'><i class='icon-remove icon-white'></i></a>&nbsp;"
                    + "<a title='Editar' class='btn btn-info' href='Editar?id=" + p.getId() + "'><i class='icon-edit icon-white'></i></a></td>");
        }
        out.print(rodape);
        out.flush();
        out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
